<template>
  <!-- 卡片 -->
  <el-card class="box-card">
    <!-- inline设置行内表单 -->
    <el-form inline :disabled="disabled">
      <!-- 一级分类 -->
      <el-form-item label="一级分类" class="form-category">
        <el-select
          :value="category1Id"
          placeholder="请选择一级分类"
          @change="getCategory2List"
        >
          <el-option
            v-for="C1 in category1List"
            :key="C1.id"
            :label="C1.name"
            :value="C1.id"
          />
        </el-select>
      </el-form-item>
      <!-- 二级分类 -->
      <el-form-item label="二级分类" class="form-category">
        <el-select
          :value="category2Id"
          placeholder="请选择二级分类"
          @change="getCategory3List"
        >
          <el-option
            v-for="C2 in category2List"
            :key="C2.id"
            :label="C2.name"
            :value="C2.id"
          />
        </el-select>
      </el-form-item>
      <!-- 三级分类 -->
      <el-form-item label="三级分类" class="form-category">
        <el-select
          :value="category3Id"
          placeholder="请选择三级分类"
          @change="SET_CATEGORY3_ID"
        >
          <el-option
            v-for="C3 in category3List"
            :key="C3.id"
            :label="C3.name"
            :value="C3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'

export default {
  name: 'CategorySelector',
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState('category', [
      'category1List',
      'category2List',
      'category3List',
      'category1Id',
      'category2Id',
      'category3Id'
    ])
  },
  mounted() {
    this.getCategory1List()
  },
  methods: {
    ...mapActions('category', [
      'getCategory1List',
      'getCategory2List',
      'getCategory3List'
    ]),
    ...mapMutations('category', ['SET_CATEGORY3_ID'])
  }
}
</script>

<style lang="scss" scoped>
.form-category {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
